package com.wbc.smartHome.web;

import com.wbc.smartHome.model.Admin;
import com.wbc.smartHome.model.Menu;
import com.wbc.smartHome.model.Result;
import com.wbc.smartHome.service.LoginService;
import com.wbc.smartHome.util.JWTUtil;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(path = "/api/loginCtl")
@Api(tags = "管理员登录模块")
public class LoginController {

    /*@Value("${admin.account}")
    private String account;*/

    @Autowired
    private LoginService loginService;

    @ApiOperation(value="管理员登录")
/*    @ApiImplicitParam(name = "admin",value = "管理员类" , required = true,type = "Admin")*/
    /*传输实体类时需要在实体类中定义*/
    @PostMapping(path = "/login")
    @ApiResponses({
            @ApiResponse(code = 200,message = "登录成功"),
            @ApiResponse(code = 201,message = "账号或密码错误"),
            @ApiResponse(code = 500,message = "系统异常")
    })
    public Result login(@RequestBody Admin admin) {//从请求体中拿到admin
        admin = loginService.login(admin);
        Result result ;
        if(admin!=null){
            admin.setAdminToken(JWTUtil.getToken(admin));
            result = new Result(200,"success",admin);
        }
        else {
            result = new Result(201,"账号或密码错误",null);
        }

        return result;
    }

    /*//通过对象接收 result返回值
    @PostMapping(path = "/login1")
    public Result login5(Admin admin, @RequestHeader("adminToken") String adminToken){
        System.out.println(adminToken);
        List<Admin> admins = loginService.login1();
        Result result = new Result(200,"success",admins);
        return result;
    }*/

    @GetMapping(path = "test")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "i" , value = "i" ,required = true,type = "String"),
            @ApiImplicitParam(name = "j" , value = "j" ,required = true,type = "String")
    })
    public String test(Integer i,Integer j){
        System.out.println("test");
        return "test";
    }

    @ApiOperation(value = "查看当前管理员权限并获取列表")
    @ApiImplicitParam(name = "adminToken",value = "管理员Token",required = true,type = "String")
    @GetMapping(path = "/menu")
    public Result menu(@RequestHeader("adminToken") String adminToken) {//从请求体中拿到admin
        List<Menu> menus = loginService.menus(adminToken);
        Result result =new Result(200,"查询成功",menus);

        return result;
    }


}
